// 队列
class Queue {
  constructor(value) {
    this.value = value
    this.length = value.length
    this.top = value.length > 0 ? 0 : -1
  }
  offer(e) {
    this.value.push(e)
    this.length += 1
    this.top = this.value.length > 0 ? 0 : -1
  }
  poll() {
    if(this.length <= 0) {
      return false
    }
    const val =  this.value.shift()
    this.length -= 1
    this.top = this.value.length > 0 ? 0 : -1
    return val
  }
  peek() {
    return this.top===-1 ? false : this.value[this.top]
  }
  size() {
    return this.length
  }
  isEmpty() {
    return this.length === 0 ? true : false
  }
}
let q = new Queue([])
q.offer(100)
q.offer(110)
q.offer(120)
console.log(q.poll())
console.log(q.peek())
console.log(q)

// 链表

class Node {
  constructor(item) {
    this.data = item
    this.next = null
  }
}

class ListNode {
  constructor() {
    this.head = null
    this.length = 0
  }
  append(value) {
    let node = new Node(value)
    if(this.head === null) {
      this.head = node
    } else {
      let current = this.head
      while(current.next) {
        current = current.next
      }
      current.next = node
    }
    this.length += 1
  }
  insert(position, value) {
    let node = new Node(value)
    if(position < 0 || position>this.length) {return false}
    if(this.head === null) {
      this.head = node
    } else {
      let currentIndex = 0
      let current = this.head
      while(currentIndex < position - 1) {
        currentIndex += 1
        current = current.next
      }
      node.next = current.next
      current.next = node
    }
    this.length += 1
  }
  removeAt(position) {
    if(position<0 || position>this.length || this.head===null) {return false}
    if(position === 0) {
      const node = this.head
      this.head = this.head.next
      this.length -= 1
      return node
    }
    let current = this.head
    let currentIndex = 0
    while(currentIndex<position - 1) {
      current = current.next
      currentIndex += 1
    }
    const node = current.next
    current.next = current.next.next
    this.length -= 1
    return node 
  }
  remove() {
    if(this.length===0) {return false}
    let current = this.head
    let currentIndex = 0
    while(currentIndex < this.length - 2) {
      current = current.next
      currentIndex += 1
    }
    const node = current.next
    current.next = null
    this.length -= 1
    return node 
  }
  get(position) {
    if(position < 0 || position > this.length) return false
    let current = this.head
    let currentIndex = 0
    while(currentIndex < position) {
      current = current.next
      currentIndex += 1
    }
    console.log(current)
    return current
  }
  indexof(value) {
    if(this.length ===0) {
      return -1
    }
    let result = -1
    let current = this.head
    let currentIndex = 0
    while(currentIndex < this.length) {
      let data = current.data
      if(data === value) {
        result = currentIndex
        break
      }
      current = current.next
      currentIndex += 1
    }
    return result
  }
  reverse() {
    let current = this.head
    let currentIndex = 0
    let next = null
    let pre = null
    while(currentIndex<this.length) {
      next = current.next
      current.next = pre
      pre = current
      current = next
      currentIndex += 1
    }
    this.head = pre
    return this.head
  }
  size() {
    return this.length
  }
  isEmpty() {
    return !this.length
  }
  getHead() {
    return this.head
  }
}
let ln = new ListNode()
ln.append(100)
ln.append(200)
ln.append(300)
ln.append(30)
ln.append(33)
ln.append(56)
ln.append(378)
ln.append(88)
ln.append(30)
ln.append(0)
// console.log(ln.removeAt(0))
// console.log(ln.remove())
// console.log(ln.get(0))
console.log(ln)
console.log(ln.size())
console.log(ln.isEmpty())
console.log(ln.indexof(30))




class MyPromise {
  constructor(func) {
    this.status = 'pending'
    this.value = null
    this.onFilfulleds = []
    let resolve = (value) => {
      if(this.status === 'pendig') {
        this.status = 'filfulled'
        this.value = value

      }
    }
    let reject = () => {

    }
    func(resolve, reject)
  }
  then(onFilfulled,onRejected) {
    if(this.status === 'pending') {
      this.onFilfulleds.push(onFilfulled)
    } else if(this.status === 'filfulled') {
      onFilfulled(this.value)
    }
  }
}
let p = new Promise((resolve, reject) => {
  resolve(234)
})
p.then(res => {
  console.log(res)
}, (err) => {
  console.log(err)
}).then(res => {
  console.log(res)
})


